import globalConst from "../../common/globalConst";
var global = globalConst.GlobalConst;

var GraphFourDirFeaturePtUtil = {}

//最东边特征点
GraphFourDirFeaturePtUtil.getEastFeaturePt = function(graph) {
    let featurePtList = [];
    graph.getMainFeaturePTLst(featurePtList);
    if (!featurePtList || (featurePtList && featurePtList.length == 0)) {
        return graph.getZone().getCnt();
    }

    //先取X最大值特征点
    let maxXPt = featurePtList[0].pt;
    let maxx = maxXPt.x;
    for (let i = 1; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (maxx < pt.x) {
            maxx = pt.x;
            maxXPt = pt;
        }
    }

    //再搜一次所有特征点中，哪些跟maxXPt的X值相等（就是位于同一条水平线上），放入临时队列中
    let sameMaxXPtLst = [];
    sameMaxXPtLst.push(maxXPt);
    for (let i = 0; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (pt == maxXPt) {
            continue;
        }
        let deta = Math.abs(maxXPt.x - pt.x);
        if (deta < global.ZERO_4E && sameMaxXPtLst.indexOf(pt) == -1) {
            sameMaxXPtLst.push(pt);
        }
    }

    //从队列中找出Y值离图形中心Y方向最近的为目标点
    let graphCntPt = graph.getZone().getCnt();
    let destPt = sameMaxXPtLst[0];
    let detaY2TwoCntLine = Math.abs(destPt.y - graphCntPt.y);
    for (let i = 1; i < sameMaxXPtLst.length; i++) {
        let pt = sameMaxXPtLst[i];
        let tmpDetaY2TwoCntLine = Math.abs(pt.y - graphCntPt.y);
        if (tmpDetaY2TwoCntLine < detaY2TwoCntLine) {
            destPt = pt;
            detaY2TwoCntLine = tmpDetaY2TwoCntLine;
        }
    }

    return destPt;
}

GraphFourDirFeaturePtUtil.getWestFeaturePt = function(graph) {
    let featurePtList = [];
    graph.getMainFeaturePTLst(featurePtList);
    if (!featurePtList || (featurePtList && featurePtList.length == 0)) {
        return graph.getZone().getCnt();
    }
    let minXPt = featurePtList[0].pt;
    let minx = minXPt.x;
    let minIdx = 0;
    for (let i = 1; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (minx > pt.x) {
            minx = pt.x;
            minXPt = pt;
            minIdx = i;
        }
    }

    let sameMinXPtLst = [];
    sameMinXPtLst.push(minXPt);
    for (let i = 0; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (pt == minXPt) {
            continue;
        }
        let deta = Math.abs(minXPt.x - pt.x);
        if (deta < global.ZERO_4E && sameMinXPtLst.indexOf(pt) == -1) {
            sameMinXPtLst.push(pt);
        }
    }

    let graphCntPt = graph.getZone().getCnt();
    let destPt = sameMinXPtLst[0];
    let detaY2TwoCntLine = Math.abs(destPt.y - graphCntPt.y);
    for (let i = 1; i < sameMinXPtLst.length; i++) {
        let pt = sameMinXPtLst[i];
        let tmpDetaY2TwoCntLine = Math.abs(pt.y - graphCntPt.y);
        if (tmpDetaY2TwoCntLine < detaY2TwoCntLine) {
            destPt = pt;
            detaY2TwoCntLine = tmpDetaY2TwoCntLine;
        }
    }

    return destPt;
}

GraphFourDirFeaturePtUtil.getSouthFeaturePt = function(graph) {
    let featurePtList = [];
    graph.getMainFeaturePTLst(featurePtList);
    if (!featurePtList || (featurePtList && featurePtList.length == 0)) {
        return graph.getZone().getCnt();
    }
    let minYPt = featurePtList[0].pt;
    let miny = minYPt.y;
    for (let i = 1; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (miny > pt.y) {
            miny = pt.y;
            minYPt = pt;
        }
    }

    let sameMinYPtLst = [];
    sameMinYPtLst.push(minYPt);
    for (let i = 0; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (pt == minYPt) {
            continue;
        }
        let deta = Math.abs(minYPt.y - pt.y);
        if (deta < global.ZERO_4E && sameMinYPtLst.indexOf(pt) == -1) {
            sameMinYPtLst.push(pt);
        }
    }

    let graphCntPt = graph.getZone().getCnt();
    let destPt = sameMinYPtLst[0];
    let detaX2TwoCntLine = Math.abs(destPt.x - graphCntPt.x);
    for (let i = 1; i < sameMinYPtLst.length; i++) {
        let pt = sameMinYPtLst[i];
        let tmpDetaX2TwoCntLine = Math.abs(pt.x - graphCntPt.x);
        if (tmpDetaX2TwoCntLine < detaX2TwoCntLine) {
            destPt = pt;
            detaX2TwoCntLine = tmpDetaX2TwoCntLine;
        }
    }

    return destPt;
}

GraphFourDirFeaturePtUtil.getNorthFeaturePt = function(graph) {
    let featurePtList = [];
    graph.getMainFeaturePTLst(featurePtList);
    if (!featurePtList || (featurePtList && featurePtList.length == 0)) {
        return graph.getZone().getCnt();
    }
    let maxYPt = featurePtList[0].pt;
    let maxy = maxYPt.y;
    for (let i = 1; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (maxy < pt.y) {
            maxy = pt.y;
            maxYPt = pt;
        }
    }
    let sameMaxYPtLst = [];
    sameMaxYPtLst.push(maxYPt);
    for (let i = 0; i < featurePtList.length; i++) {
        let pt = featurePtList[i].pt;
        if (pt == maxYPt) {
            continue;
        }
        let deta = Math.abs(maxYPt.y - pt.y);
        if (deta < global.ZERO_4E && sameMaxYPtLst.indexOf(pt) == -1) {
            sameMaxYPtLst.push(pt);
        }
    }

    let graphCntPt = graph.getZone().getCnt();
    let destPt = sameMaxYPtLst[0];
    let detaX2TwoCntLine = Math.abs(destPt.x - graphCntPt.x);
    for (let i = 1; i < sameMaxYPtLst.length; i++) {
        let pt = sameMaxYPtLst[i];
        let tmpDetaX2TwoCntLine = Math.abs(pt.x - graphCntPt.x);
        if (tmpDetaX2TwoCntLine < detaX2TwoCntLine) {
            destPt = pt;
            detaX2TwoCntLine = tmpDetaX2TwoCntLine;
        }
    }
    return destPt;
}

export {GraphFourDirFeaturePtUtil}